package com.example.rfidtouristsclient;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rfidtouristsclient.utils.NetHelper;
import com.example.rfidtouristsclient.view.ArcProgressBar;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class IntegralSelectActivity extends AppCompatActivity {

    private ArcProgressBar pb_sd = null;
    private int integral;
    private String serverIP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //允许在主线程中放行代码
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        String integral;
        while(true){
            Intent intent = getIntent();
            integral = intent.getStringExtra("integral");
            System.out.println("jifen:"+integral);
            //如果积分为空则阻塞
            if(integral==null){}else break;
        }

        setContentView(R.layout.activity_integralselect);
        //获得积分的数据信息
        this.initData(integral);
    }

    //读取本地文件来获取id
    private String getID() {
        BufferedReader bufferedReader = null;
        String value = null;
        try {
            //访问内部存储
            File dataDirectory = new File(getFilesDir(), "userinfo");
            //以字符流读取文件信息
            bufferedReader = new BufferedReader(new FileReader(dataDirectory));
            String info;
            while ((info = bufferedReader.readLine()) != null) {
                if (info.startsWith("id")) {
                    value = info.substring(info.indexOf(":") + 1);
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return value;
    }

    private void initData(String data) {

        pb_sd = findViewById(R.id.pb_sd);
        //进度条最大值
        pb_sd.setMax(1000);
        pb_sd.setTitle("积分");
        //将string类型的积分转化为int类型
        double inte = Double.valueOf(data);
        integral = (int) inte;
        //数据的动态显示
        new MyAsyncSDTask().execute(0);
    }

    private class MyAsyncSDTask extends AsyncTask<Integer, Integer, Integer> {

        @Override
        protected Integer doInBackground(Integer... integers) {
            Integer timer = 0;
            while (timer <= integral) {
                try {
                    publishProgress(timer);//更新进度
                    timer++;         //timer的值自增
                    Thread.sleep(1); //延迟5毫秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            pb_sd.setProgress((int) (values[0]));//设置ArcProgressBar的进度
        }

    }


}
